﻿using Aliyun.OpenServices.OpenStorageService;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Croton.Common.Helper
{
    public class OssHelper
    {
        static string BUCKETNAME = ConfigHelper.GetString("BucketName");
        static string ACCESSID = ConfigHelper.GetString("AccessId");
        static string ACCESSKEY = ConfigHelper.GetString("AccessKey");
        public static string UploadFile(Stream streem, string fullname, string contenttype)
        {
            try
            {
                var client = new OssClient(ACCESSID, ACCESSKEY);
                var metadata = new ObjectMetadata();
                metadata.ContentType = contenttype;
                var ret = client.PutObject(BUCKETNAME, fullname, streem, metadata);
                return  "http://" + BUCKETNAME + ".oss.aliyuncs.com/" + fullname;
            }
            catch (Exception ex)
            { 
                //Log.Exceptin(ex)
                Console.WriteLine(ex.Message);
                return "";
            } 
        }

        public static string CheckFileExists(string fullname)
        {
            var client = new OssClient(ACCESSID, ACCESSKEY);
            var obj = client.GetObject(BUCKETNAME, fullname);
            if (obj != null)
                return "http://" + BUCKETNAME + ".oss.aliyuncs.com/" + fullname;
            else
            {
                return "";
            }
        }
    }
}
